<?php
/**
 * Created by : PhpStorm
 * Message: 后台首页控制器
 * User: bingdi
 * Time: 2020/2/1814:42
 * motto: 现在的努力是为了小时候吹过的牛逼!
 */

namespace app\admin\controller;


use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\route\dispatch\Controller;

class Report extends AdminBase
{
    /**\
     * 待审核
     */
    public function index()
    {
        $model = Db::name('report')
            ->where('status',1)
            ->order(['id' => 'desc'])
            ->column('id,content_id,type,create_time,report_id,status');
        foreach ($model as $k=>$v) {
            $model[$k]['create_time']=date("Y-m-d H:i:s",$v['create_time']);
            $model[$k]['type']=\app\common\model\Report::TYPE[$v['type']];
            $model[$k]['report_id']=\app\common\model\Report::REPORT[$v['report_id']];
        }
        return View::fetch("index", ['model' => $model]);
    }
    /**\
     * 已审核
     */
    public function index_check()
    {
        $model = Db::name('report')
            ->order(['id' => 'desc'])
            ->column('id,content_id,type,create_time,report_id,status');
        return View::fetch("index", ['model' => $model]);
    }
    /**\
     * 查看内容
     */
    public function show(){
        $id=input('get.id');
        $report=Db::name('report')->where('id',$id)->find();
        if ($report['type']==1){
            $model=Db::name('speech')
                ->where('id',$report['content_id'])
                ->find();
            $model['img']=\Qiniu\json_decode($model['img']);
        }else{
            $model=Db::name('comment')
                ->where('id',$report['content_id'])
                ->find();
        }
        return View::fetch('show',['model'=>$model]);
    }
    /**|
     * 删除违规内容
     */
    public function delete(){
        $id=input('get.id');
        Db::startTrans();
        try {
            $report=Db::name('report')
                ->where(['id'=>$id,'status'=>1])
                ->lock(true)
                ->find();
            if (!$report){
                return show(0,'删除失败');
            }
            Db::name('report')
                ->where(['content_id'=>$report['content_id'],'status'=>1,'type'=>$report['type']])
                ->update(['status'=>2,'update_time'=>time()]);
            if ($report['type']==1){
                $name="speech";
            }else{
                $name="comment";
            }
            Db::name($name)
                ->where('id',$report['content_id'])
                ->delete();
            Db::commit();
            return show(1, '删除成功');
        } catch (\Exception $e) {
            Db::rollback();
            return show(0, $e->getMessage());
        }
    }
}